﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BomberManServer.Gaming.Object.Bonuses;

namespace BomberManServer.Gaming.Object.Mapping.Items
{
    // we have one BonusBlock per zone per type
    public class BonusBlock : Destructible
    {
        private Bonus.Type type;
        private BonusPerm bonus;
        // we need to know the zone
        // maybe not if we count chance drops in each Item per Type
        private Zone zone;
        private uint blocks_remaining;
        private uint bonus_remaining;
        private uint block_max;
        private uint bonus_max;
        // modifier that makes you more lucky to find 
        private uint luck_balancer;
        public BonusBlock(Game game,uint block_max, uint bonus_max, Bonus.Type type):base(game)
        {
            this.type = type;
            this.block_max = block_max;
            this.bonus_max = bonus_max;
            bonus = new BonusPerm(game,type);
        }

        public void reset()
        {
            this.bonus_remaining = bonus_max;
            this.blocks_remaining = block_max;
            luck_balancer = 0;
        }

        public override Item actionDeflagration(Deflagration deflagration)
        {
            if (try_spawn())
            {
                game.sender.onBonusDrop(deflagration.coord);
                return bonus;
            }
            else
            {
                base.actionDeflagration(deflagration);
                return Items.None.instance;
            }
        }

        // returns true if a bonus spawns
        public bool try_spawn() {
            if (bonus_remaining == 0)
                return false;

            if (game.random.NextUInt(blocks_remaining) + luck_balancer < bonus_remaining + luck_balancer)
            {
                luck_balancer = 0;
                blocks_remaining--;
                bonus_remaining--;
                return true;
            }
            luck_balancer++;
            blocks_remaining--;
            return false;
        }
    }
}
